package cn.iocoder.yudao.module.course.service.courses;

import cn.iocoder.yudao.module.course.controller.admin.coursematerials.vo.CourseMaterialsRespVO;
import cn.iocoder.yudao.module.course.controller.admin.coursematerials.vo.CourseMaterialsSaveReqVO;
import cn.iocoder.yudao.module.course.dal.dataobject.coursematerials.CourseMaterialsDO;
import cn.iocoder.yudao.module.course.dal.mysql.coursematerials.CourseMaterialsMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.course.controller.admin.courses.vo.*;
import cn.iocoder.yudao.module.course.dal.dataobject.courses.CoursesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.course.dal.mysql.courses.CoursesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.course.enums.ErrorCodeConstants.*;

/**
 * 课程 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CoursesServiceImpl implements CoursesService {

    @Resource
    private CoursesMapper sMapper;

    @Resource
    private CourseMaterialsMapper courseMaterialsMapper;

    @Override
    public Long creates(CoursesSaveReqVO createReqVO) {
        // 插入
        CoursesDO s = BeanUtils.toBean(createReqVO, CoursesDO.class);
        sMapper.insert(s);
        addMaterials(s,createReqVO);
        // 返回
        return s.getId();
    }

    @Override
    public void updates(CoursesSaveReqVO updateReqVO) {
        // 校验存在
        validatesExists(updateReqVO.getId());
        // 更新
        CoursesDO updateObj = BeanUtils.toBean(updateReqVO, CoursesDO.class);
        sMapper.updateById(updateObj);
        courseMaterialsMapper.delete(CourseMaterialsDO::getCourseId,updateObj.getId());
        addMaterials(updateObj,updateReqVO);
    }

    private void addMaterials(CoursesDO s,CoursesSaveReqVO updateReqVO) {
        List<CourseMaterialsSaveReqVO> materialList = updateReqVO.getMaterialList();
        if(!CollectionUtils.isEmpty(materialList)){
            List<CourseMaterialsDO> cmdos = Lists.newArrayList();
            for (CourseMaterialsSaveReqVO courseMaterialsSaveReqVO : materialList.stream().filter(item -> item.getMaterialId() != null).collect(Collectors.toSet())) {
                CourseMaterialsDO cmdo = new CourseMaterialsDO(null,s.getId(),courseMaterialsSaveReqVO.getMaterialId(),courseMaterialsSaveReqVO.getOrderIndex());
                cmdos.add(cmdo);
            }
            if(!CollectionUtils.isEmpty(cmdos)){
                courseMaterialsMapper.insertBatch(cmdos);
            }
        }
    }

    @Override
    public void deletes(Long id) {
        // 校验存在
        validatesExists(id);
        // 删除
        sMapper.deleteById(id);
        courseMaterialsMapper.delete(CourseMaterialsDO::getCourseId,id);
    }

    private void validatesExists(Long id) {
        if (sMapper.selectById(id) == null) {
            throw exception(COURSE_NOT_EXISTS);
        }
    }

    @Override
    public CoursesRespVO gets(Long id) {
        CoursesDO coursesDO = sMapper.selectById(id);
        CoursesRespVO respVO = BeanUtils.toBean(coursesDO, CoursesRespVO.class);
        List<CourseMaterialsDO> courseMaterialsDOS = courseMaterialsMapper.selectList(CourseMaterialsDO::getCourseId, id);
        respVO.setMaterialList(BeanUtils.toBean(courseMaterialsDOS, CourseMaterialsRespVO.class));
        return respVO;
    }

    @Override
    public PageResult<CoursesDO> getsPage(CoursesPageReqVO pageReqVO) {
        return sMapper.selectPage(pageReqVO);
    }

}